generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["fullTextSearch", "fullTextIndex"]
}

datasource db {
  provider     = "postgresql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

model Course {
  id          String  @id @default(uuid())
  userId      String
  title       String  @db.Text
  description String? @db.Text
  imageUrl    String? @db.Text
  price       Float?
  isPublished Boolean @default(false)

  chapters    Chapter[]
  purchases   Purchase[]
  attachments Attachment[]

  categoryId String?
  category   Category? @relation(fields: [categoryId], references: [id])

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Category {
  id   String @id @default(uuid())
  name String @unique

  courses Course[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Attachment {
  id   String @id @default(uuid())
  name String
  url  String @db.Text

  course   Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
  courseId String

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Chapter {
  id          String  @id @default(uuid())
  title       String
  description String? @db.Text
  videoUrl    String? @db.Text
  position    Int
  isPublished Boolean @default(false)
  isFree      Boolean @default(false)

  muxData      MuxData?
  userProgress UserProgress[]

  courseId String
  course   Course @relation(fields: [courseId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model MuxData {
  id         String  @id @default(uuid())
  assetId    String
  playbackId String?

  chapterId String  @unique
  chapter   Chapter @relation(fields: [chapterId], references: [id], onDelete: Cascade)
}

model UserProgress {
  id          String  @id @default(uuid())
  userId      String
  isCompleted Boolean @default(false)

  chapterId String
  chapter   Chapter @relation(fields: [chapterId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([userId, chapterId])
}

model Purchase {
  id     String @id @default(uuid())
  userId String

  courseId String
  course   Course @relation(fields: [courseId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([userId, courseId])
}

model StripeCustomer {
  id               String @id @default(uuid())
  userId           String @unique
  stripeCustomerId String @unique

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
